package com.atguigu.service;

import com.atguigu.domain.Architect;
import com.atguigu.domain.Designer;
import com.atguigu.domain.Employee;
import com.atguigu.domain.Programmer;

/**
 * @author : leimingchao
 * @date Date : 2021年09月15日 22:27
 */
public class TeamService {

    /**
     * 给memberId赋值
     */
    private static int counter = 1;

    /**
     * 限制开发团队的人数
     */
    private final int MAX_MEMBER = 5;

    /**
     * 保存开发团队成员
     */
    private Programmer[] team = new Programmer[MAX_MEMBER];

    /**
     * 开发团队中实际人数
     */
    private int total;

    public TeamService() {
    }

    public Programmer[] getTeam() {

        Programmer[] team = new Programmer[total];
        int length = team.length;

        for (int i = 0; i < length; i++) {

            team[i] = this.team[i];
        }
        return team;
    }

    public void addMember(Employee e) throws TeamException {

        if (total >= MAX_MEMBER) {
            throw new TeamException("成员已满，无法添加");
        }

        if (!(e instanceof Programmer)) {
            throw new TeamException("该员工不是开发人员，无法添加");
        }

        if (isExist(e)) {
            throw new TeamException("该员工已在开发团队中");
        }

        Programmer programmer = (Programmer) e;

        // if (Status.BUSY.getNAME().equals(programmer.getStatus().getNAME())) {
        //
        //     throw new TeamException("该员工已是某团队成员");
        //
        // } else if (Status.VOCATION.getNAME().equals(programmer.getStatus().getNAME())) {
        //
        //     throw new TeamException("该员工正在休假，无法添加");
        // }
        //byte\short\char\int\String\枚举类对象
        switch (programmer.getStatus()) {
            case BUSY:
                throw new TeamException("该员工已是某团队成员");
            case VOCATION:
                throw new TeamException("该员工正在休假，无法添加");
        }

        int numOfArch = 0, numOfDes = 0,numOfPro = 0;

        for (int i = 0; i < total; i++) {

            if (team[i] instanceof Architect) {
                numOfArch++;
            } else if (team[i] instanceof Designer) {
                numOfDes++;
            } else if (team[i] instanceof Programmer){
                numOfPro++;
            }
        }

        if (programmer instanceof Architect) {
            if (numOfArch >= 1) {
                throw new TeamException("团队中至多只能有一名架构师");
            }
        } else if (programmer instanceof Designer) {
            if (numOfDes >= 2) {
                throw new TeamException("团队中至多只能有两名设计师");
            }
        } else if (programmer instanceof Programmer) {
            if (numOfPro >= 3) {
                throw new TeamException("团队中至多只能有三名程序员");
            }
        }

        team[total++] = programmer;

        programmer.setStatus(Status.BUSY);
        programmer.setMemberId(counter++);

    }

    private boolean isExist(Employee e) {

        for (int i = 0; i < total; i++) {

            if (team[i].getId() == e.getId()) {
                return true;
            }
        }
        return false;
    }

    public void removeMember(int memberId) throws TeamException {

        int i = 0;
        for (; i < total; i++) {
            if (team[i].getMemberId() == memberId) {
                team[i].setStatus(Status.FREE);
                break;
            }
        }

        if (i == total) {

            throw new TeamException("找不到指定memberId的员工，删除失败");
        }

        for (int j = i + 1; j < total;j++) {

            team[j - 1] = team[j];
        }

        team[--total] = null;

    }
}
